package com.ruoyi.common.config.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

/**
 * @JsonDeserialize(using = StringToLongDeserializer.class)  // 反序列化时从字符串转换为 Long
 * @author huixh
 */
public class StringToLongDeserializer extends JsonDeserializer<Long> {

    @Override
    public Long deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
        String value = jsonParser.getText();
        try {
            // 将字符串转换为 Long
            return Long.valueOf(value);
        } catch (NumberFormatException e) {
            return null; // 如果转换失败，可以返回 null 或者抛出异常
        }
    }
}